package com.weipu.common.facade.support.service.task;

import com.weipu.common.facade.exception.DxException;
import com.weipu.common.facade.factory.ProviderFactory;
import com.weipu.common.util.Logger;

/**
 * 解锁订单任务任务
 */
public class UnlockOrderTask extends BaseServiceTask<Boolean>
{
    private String orderNumber;//订单号码
    
    private String notes;//备注，可以为空
    
    private String rquestUserName;//用户名，可以为空
    
    public UnlockOrderTask(String orderNumber, String notes, String rquestUserName)
    {
        this.orderNumber = orderNumber;
        this.notes = notes;
        this.rquestUserName = rquestUserName;
    }
    
    /**
     * 
     */
    private static final long serialVersionUID = 8419058866945901566L;
    
    @Override
    protected boolean executeRequest()
    {
        return true;
    }
    
    @Override
    protected Boolean execute()
    {
        
        int reTryCount = 0;
        
        String result = null;//接口返回结果
        do
        {
            reTryCount++;
            
            result = unlockOrder();
            
            //重试次数超过3次，认为解锁失败，认为任务失败
            if (reTryCount > 3)
            {
                break;
            }
            
        } while (!"0".equals(result));//只要不成功则会重试解锁订单，最多尝试3此
        
        return true;
    }
    
    //执行订单解锁
    private String unlockOrder()
    {
        try
        {
            ProviderFactory.createOrderServiceProvider().EditionOrder(orderNumber, notes, rquestUserName);
        }
        catch (DxException e)
        {
            Logger.e(TAG, "EditionOrder error, DX code: " + e.getErrCode(), e);
            return e.getErrCode();
        }
        catch (Exception e)
        {
            Logger.e(TAG, "EditionOrder error.", e);
            return "-1";
        }
        return "0";
    }
    
    //    equals 返回 true的链各个对象则hashcode必须相等
    @Override
    public int hashCode()
    {
        return toString().hashCode();
    }
    
    //提供Equals对比，单例任务(同一时间只需运行一个)
    @Override
    public String toString()
    {
        StringBuffer sb = new StringBuffer();
        sb.append("orderNumber: ").append(orderNumber).append(',');
        sb.append("rquestUserName: ").append(rquestUserName).append(',');
        sb.append("notes: ").append(notes);
        return sb.toString();
    }
    
    @Override
    protected void executeResponse(Boolean result)
    {
        super.executeResponse(result);
    }
}
